import { _decorator, BoxCollider2D, Component, EventTouch, Input, instantiate, Node } from 'cc';
import { BaseComponent } from '../Manager/UIManager/BaseComponent';
import { GAME_POP, GAME_PREFAB, GAME_TOP, SOUND_TYPE } from '../Data/GlobalData';
import { PopSingleCollect } from './PopSingleCollect';
import { HeadSkinInfo } from '../Data/GlobaInterface';
import { PopHeadSkin } from './PopHeadSkin';
import { TopPromptText } from '../Top/TopPromptText';
import { PopBuy } from './PopBuy';
const { ccclass, property } = _decorator;

@ccclass('PopCollect')
/**
 * 顶部收藏层
 */
export class PopCollect extends BaseComponent {

    //食物层
    @property(Node)
    private FoodLoad: Node = null;
    //食物按钮
    @property(Node)
    private FoodBtn: Node = null;

    //鸟头层
    @property(Node)
    private HeadLoad: Node = null;
    //鸟头按钮
    @property(Node)
    private HeadBtn: Node = null;

    //收藏数组
    private CollectArr: Node[] = [];

    //收藏层
    @property(Node)
    private CollectLoad: Node = null;

    //头部层
    @property(Node)
    private SkinNode: Node = null;

    //本地缓存Key
    private SkinKey: string = "SkinKey";

    //皮肤信息
    public SkinInfo: HeadSkinInfo[] = [];

    //更新金币方法
    public UpdateCoin: Function = null;

    //默认
    private DefaultSkin: HeadSkinInfo[] = [
        { Index: 0, Price: 0, IsBuy: true },
        { Index: 1, Price: 100, IsBuy: false },
        { Index: 2, Price: 300, IsBuy: false },
        { Index: 3, Price: 500, IsBuy: false },
        { Index: 4, Price: 1000, IsBuy: false },
        { Index: 5, Price: 1000, IsBuy: false },
    ];

    public SkinArr: Node[] = [];

    protected Init(): void {
        for (let i = 0; i < 12; i++) {
            let collect: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.PopSingleCollect])
            this.CollectLoad.addChild(collect);
            collect.getComponent(PopSingleCollect).Init(i)
            this.CollectArr.push(collect);
        }
        //更新头部信息
        if (app.dataManager.GetStorage(this.SkinKey)) {
            this.SkinInfo = app.dataManager.GetStorage(this.SkinKey) as HeadSkinInfo[];
        } else {
            this.SkinInfo = this.DefaultSkin;
            app.dataManager.SetStorage(this.SkinKey, this.SkinInfo);
        }
        //
        for (let si of this.SkinInfo) {
            let psbs: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.PopHeadSkin]);
            psbs.getComponent(PopHeadSkin).UpdateBallInfo(si, this.SkinInfo.indexOf(si));
            psbs.on(Input.EventType.TOUCH_START, this.SkinClick, this);
            psbs.parent = this.SkinNode;
            this.SkinArr.push(psbs);
        }
    }

    /**
    * 点击皮肤
    */
    private SkinClick(_e: EventTouch) {
        let skin: Node = _e.currentTarget;

        if (skin.getComponent(PopHeadSkin).Info.IsBuy) {//选择
            app.gameData.GameData.basket = skin.getComponent(PopHeadSkin).Info.Index;
            app.gameData.UpdateGameDateLocal();
            this.UpdateSelect();
        } else {
            if (app.gameData.GameData.coin < skin.getComponent(PopHeadSkin).Info.Price) {//提示不能购买
                app.topManager.ShowTop(GAME_TOP.TopPromptText, false);
                app.topManager.CurrentTop.getComponent(TopPromptText).UpdatePrompt("金币不足");
            } else {//弹窗提示是否购买
                app.topManager.ShowTop(GAME_POP.PopBuy);
                app.topManager.CurrentTop.getComponent(PopBuy).UpdateStyle(
                    skin.getComponent(PopHeadSkin).Info,
                    skin.getComponent(PopHeadSkin).MyIndex
                );
                app.topManager.CurrentTop.getComponent(PopBuy).SkinBox = this.node;
            }
        }
    }

    /**
         * 更新选择样式
         */
    public UpdateSelect() {
        for (let si of this.SkinArr) {
            si.getComponent(PopHeadSkin).RightIcon.active = (si.getComponent(PopHeadSkin).Info.Index == app.gameData.GameData.basket);
            si.getComponent(PopHeadSkin).JudgeShow();
        }
    }

    protected OnShow(): void {

        for (let i = 0; i < this.CollectLoad.children.length; i++) {
            this.CollectLoad.children[i].getComponent(PopSingleCollect).IsHave(i < app.gameData.GameData.currentlevel);
        }

        this.UpdateShow(true);
        this.UpdateSelect();
    }

    /**
     * 点击关闭
     */
    private CloseClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        app.popManager.HidePop(GAME_POP.PopCollect)
    }

    /**
     * 更新显示层
     */
    private UpdateShow(_isfood: boolean) {

        this.FoodLoad.active = _isfood;
        this.FoodBtn.active = _isfood;

        this.HeadLoad.active = !_isfood;
        this.HeadBtn.active = !_isfood;

    }

    /**
     * 点击食物按钮
     */
    private FoodClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        this.UpdateShow(true);

    }

    /**
     * 点击头像按钮
     */
    private HeadClick() {
        app.audioManager.PlayEffect(SOUND_TYPE.Button);
        this.UpdateShow(false);
    }

    /**
    * 更新本地缓存
    */
    public UpdateLocalStorage(_myindex: number) {

        for (let si of this.SkinInfo) {
            if (si.Index == _myindex) {
                si.IsBuy = true;
            }
        }
        this.UpdateCoin && this.UpdateCoin();
        app.dataManager.SetStorage(this.SkinKey, this.SkinInfo);
        this.UpdateSelect();
    }

}


